#include "Control.h"
#include "Utils/Common.h"

namespace AVSAnalyzer {

    bool Control::parseRecognitionRegion() 
    {
        bool res = false;
        if (recognitionRegion_points.empty()) 
        {
            if (videoWidth > 0 && videoHeight > 0) 
            {
                std::vector<std::string> xys = split(recognitionRegion, ",");
                int lineCount = xys.size() / 2; 		//多边形边数
                if (lineCount > 3) {
                    int x_index;
                    int y_index;
                    int x;
                    int y;

                    for (int i = 0; i < lineCount; i++)
                    {
                        x_index = i * 2;
                        y_index = x_index + 1;
                        x = stof(xys[x_index]) * videoWidth;
                        y = stof(xys[y_index]) * videoHeight;

                        recognitionRegion_d.push_back(x);
                        recognitionRegion_d.push_back(y);

                        cv::Point p(x, y);
                        recognitionRegion_points.push_back(p);
                    }
                    res = true;
                }
            }
        }
        else 
        {
            res = true;
        }
        return res;
    }

    bool Control::validateAdd(std::string& result_msg) 
    {
        if (code.empty() || streamUrl.empty() || algorithmCode.empty() || recognitionRegion.empty()) 
        {
            result_msg = "validate parameter error";
            return false;
        }
        if (pushStream) 
        {
            if (pushStreamUrl.empty()) 
            {
                result_msg = "validate parameter pushStreamUrl is error: " + pushStreamUrl;
                return false;
            }
        }
        result_msg = "validate success";
        return true;
    }

    bool Control::validateCancel(std::string& result_msg) {
        if (code.empty()) {
            result_msg = "validate parameter error";
            return false;
        }
        result_msg = "validate success";
        return true;
    }
}